﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Collections;
using Microsoft.Extensions.Configuration;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using Dapper;
using Dapper.Contrib.Extensions;
using System.ComponentModel;

namespace Casbin.NET.Adapter.Dapper
{
    /// <summary>
    /// 数据库
    /// </summary>
    public class DapperDBContext
    {
        /// <summary>
        /// 数据库类型
        /// </summary>
        public enum DatabaseType
        {
            [Description("MYSQL数据库")]
            MYSQL,

            [Description("SQLserver数据库")]
            MSSQL,

            [Description("SQLite数据库")]
            SQLITE,

            [Description("PGSQL(大象)数据库")]
            PGSQL
        };

        /// <summary>
        /// 主键类型
        /// </summary>
        public enum PrimaryType
        {
            [Description("int类型")]
            intType,

            [Description("Guid(char(36))类型")]
            GuidType
        };

        private readonly string connectionString;
        private readonly DatabaseType dbType;

        public DapperDBContext(DatabaseType databaseType, string dbConnectionString)
        {
            connectionString = dbConnectionString;
            dbType = databaseType;
        }

        /// <summary>
        /// 创建连接
        /// </summary>
        /// <returns></returns>
        public IDbConnection CreateConnection()
        {
            switch (dbType)
            {
                case DatabaseType.MYSQL:
                    return new MySqlConnection(connectionString);

                case DatabaseType.MSSQL:
                    return new SqlConnection(connectionString);

                //case DatabaseType.SQLITE:
                //    return new SqlConnection(connectionString);

                //case DatabaseType.PGSQL:
                //    return new SqlConnection(connectionString);

                default: return new MySqlConnection(connectionString);
            };
        }
    }
}